package com.seari.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.seari.bean.PostID;
import com.seari.bean.TableSplitResult;
import com.seari.bean.Tablepar;

@Controller
public class PostIdController {
	public Map<String, PostID> dataBase = new HashMap<>();

	@GetMapping("postList")
	public String view() {
		return "postList";
	}


	@RequestMapping("postList/addData")
	@ResponseBody
	public Map<String, Object> addData(PostID PostIDInfo) {

		dataBase.put(PostIDInfo.getId(), PostIDInfo);

		Map<String, Object> res = new HashMap<String, Object>();
		res.put("code", 200);
		res.put("msg", "成功");
		return res;
	}

	/**
	 * 修改角色
	 * 
	 * @param id
	 * @param mmap
	 * @return
	 */
	@RequestMapping("postList/editData")
	@ResponseBody
	public Map<String, Object> edit(PostID PostIDInfo) {

		dataBase.put(PostIDInfo.getId(), PostIDInfo);

		Map<String, Object> res = new HashMap<String, Object>();
		res.put("code", 200);
		res.put("msg", "成功");
		return res;
	}

	@RequestMapping("postList/list")
	@ResponseBody
	public Object getData(Tablepar tablepar) {
		List<PostID> list = new ArrayList<>();

		Set<Entry<String, PostID>> entrySet = dataBase.entrySet();
		Iterator<Entry<String, PostID>> iterator = entrySet.iterator();
		while (iterator.hasNext()) {
			Entry<String, PostID> next = iterator.next();
			list.add(next.getValue());
		}

		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		PageInfo<PostID> pageInfo = new PageInfo<PostID>(list);
		TableSplitResult<PostID> result = new TableSplitResult<PostID>(pageInfo.getPageNum(), pageInfo.getTotal(),
				pageInfo.getList());
		return result;
	}

	@RequestMapping("postList/remove")
	@ResponseBody
	public Map<String, Object> delete(@RequestBody List<String> ids) {
		for (String id : ids) {
			dataBase.remove(id);
		}
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("code", 200);
		res.put("msg", "成功");
		return res;
	}

	public void initPostDataBase() {
		PostID post1 = new PostID();
		post1.setId("1000001");
		post1.setPost("值班站长");
		post1.setShift("日班");
		post1.setArea("全站");

		PostID post2 = new PostID();
		post2.setId("1000002");
		post2.setPost("行车值班员");
		post2.setShift("早班");
		post2.setArea("工作区域");

		PostID post3 = new PostID();
		post3.setId("1000003");
		post3.setPost("设备值班员");
		post3.setShift("日班");
		post3.setArea("设备室");

		PostID post4 = new PostID();
		post4.setId("1000004");
		post4.setPost("补票岗站务员");
		post4.setShift("日班");
		post4.setArea("站厅付费区");

		PostID post5 = new PostID();
		post5.setId("1000005");
		post5.setPost("巡视岗站务员(机动)");
		post5.setShift("中班");
		post5.setArea("全站");

		PostID post6 = new PostID();
		post6.setId("1000006");
		post6.setPost("客服岗站务员");
		post6.setShift("中班");
		post6.setArea("站厅付费区");

		PostID post7 = new PostID();
		post7.setId("1000007");
		post7.setPost("站台岗");
		post7.setShift("中班");
		post7.setArea("站台");

		dataBase.put("1000001", post1);
		dataBase.put("1000002", post2);
		dataBase.put("1000003", post3);
		dataBase.put("1000004", post4);
		dataBase.put("1000005", post5);
		dataBase.put("1000006", post6);
		dataBase.put("1000007", post7);
		// dataBase.put("1000008", PostID8);
		// dataBase.put("1000009", PostID9);
		// dataBase.put("10000010", PostID10);
		// dataBase.put("10000011", PostID11);
		// dataBase.put("10000012", PostID12);
		// dataBase.put("10000013", PostID13);
		// dataBase.put("10000014", PostID14);
		// dataBase.put("10000015", PostID15);

	}

	public List<PostID> getData() {
		List<PostID> list = new ArrayList<>();
		Set<Entry<String, PostID>> entrySet = dataBase.entrySet();
		Iterator<Entry<String, PostID>> iterator = entrySet.iterator();
		while (iterator.hasNext()) {
			Entry<String, PostID> next = iterator.next();
			list.add(next.getValue());
		}
		return list;
	}

	public static void main(String[] args) {
	}
}
